package descentFly.weaponts
{
	import UIFree.ObjectEvent;
	
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.Sprite3D;
	import away3d.materials.MaterialBase;
	
	import descentFly.away3D4.chips.df_chipAway;
	import descentFly.away3D4.chips.df_chipModel;
	import descentFly.chips.df_chip;
	import descentFly.df_optionsObject;
	import descentFly.df_space;
	import descentFly.sound.df_soundManager;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	import flash.utils.Timer;
	
	import space.sp_utils;
	
	public class df_pulseElement extends ObjectContainer3D implements IAwayBullet {
		public static const FIELD_DAMAGE: String 	= 'damage';
		public static const FIELD_SPEED: String 	= 'speed';
		public static const FIELD_DISTANCE: String 	= 'distance';
		
		public static const DEFAULT_DAMAGE: Number 	= 1;
		public static const DEFAULT_SPEED: Number 	= 1;
		public static const DEFAULT_DISTANCE: Number = 1000;
		
		private var _timer			: Timer;
		private var _options		: Object;
		private var _mt				: Number;
		private var _chip			: df_chipAway;
		private var _prevPosition	: Vector3D;
		
		public function df_pulseElement(a_chip: df_chipAway, a_options:Object) {
			a_options = sp_utils.checkParam(a_options, FIELD_DAMAGE, DEFAULT_DAMAGE);
			a_options = sp_utils.checkParam(a_options, FIELD_SPEED, DEFAULT_SPEED);
			a_options = sp_utils.checkParam(a_options, FIELD_DISTANCE, DEFAULT_DISTANCE);
			super();
			_chip	= a_chip;
			_timer 	= new Timer(20);
			_timer.addEventListener(TimerEvent.TIMER, doTimer);
			_options = a_options;
			_mt = 0;
			initialize();
		}
		
		protected function initialize(): void {
		}
		
		public function start(startTransform: Matrix3D): void {
			transform = startTransform;	
			_timer.start();
			playSound('start');
		}
		
		protected function get options(): Object {
			return _options;
		}
		
		public function playSound(soundName: String, refTransf: Object=null): void {
			if (options.sounds && options.sounds.hasOwnProperty(soundName))
				df_soundManager.playSound(options.sounds[soundName], refTransf?refTransf:this);
		}
	
		protected function update(): void {
			_prevPosition = position.clone();
			moveForward(_options.speed);
			_mt += _options.speed;
			if (_mt >= _options.distance) dispose(true);
			else _chip.space.dispatchEvent(new ObjectEvent(df_space.CHECKBULLETHIT, this))
		}
		
		public function get ray() : Vector3D {
			return _prevPosition.subtract(position);
		}
		
		public function hitBullet(position		: Matrix3D): void {
			playSound('hitChip', position);
			dispose(true);	
		}
		
		override public function dispose(deep: Boolean): void {
			_timer.stop();
			_timer.removeEventListener(TimerEvent.TIMER, doTimer);
			super.dispose(deep);
		}
		
		protected function doTimer(e: Event): void {
			update();
		}
		
		public function get chip(): df_chip {
			return _chip;
		}
		
		public function get damage(): Number {
			return _options.damage;
		}
	}
}